import { showToast, typeOf, toCssVal } from "utils/index";
const baseUrl = "./imgs";
const map = {
  coupon: {
    tips: "暂无优惠券",
    icon: baseUrl + "/no-coupon.png",
  },
  data: {
    tips: "空空如也~",
    icon: baseUrl + "/no-data.png",
    // btnText: "返回",
    // btnUrl: "back", //页面地址、函数、back（会执行wx.navigateBack()）、reload（重新加载本页面）
  },
  msg: {
    tips: "暂未收到系统消息",
    icon: baseUrl + "/no-msg.png",
  },
  network: {
    tips: "网络异常，请重试\n（0-1009）",
    icon: "../../assets/img/local/no-network.png",
    btnText: "重试",
    btnUrl: "back",
  },
  order: {
    tips: "暂无订单",
    icon: baseUrl + "/no-order.png",
  },
  search: {
    tips: "被你难倒了，换个词试试吧~",
    icon: baseUrl + "/no-search.png",
  },
  fail: {
    tips: "服务器访问失败，努力修复中~",
    icon: baseUrl + "/fail.png",
  },
};
const topMap = {
  pageCenter: "50vh",
  parentCenter: "50%",
};
Component({
  options: {
    styleIsolation: "apply-shared",
  },
  properties: {
    //距离顶部的距离
    top: {
      type: String,
      value: "80", //css支持的值, pageCenter, parentCenter
    },
    //图标宽度，高度会自适应
    iconWidth: {
      type: String,
      value: "360rpx",
    },
    //空状态类型，可选值：coupon
    type: {
      type: String,
      value: "data",
      validator(val) {
        return [
          "coupon",
          "data",
          "msg",
          "network",
          "order",
          "search",
          "500",
        ].includes(val);
      },
    },
    //图标url
    icon: {
      type: String,
      value: "",
    },
    //提示语
    notice: {
      type: String,
      value: "",
    },
    //提示语
    tips: {
      type: String,
      value: "",
    },
    //按钮文本
    btnText: {
      type: String,
      value: "",
    },
    //按钮跳转地址
    btnUrl: {
      type: String,
      value: "",
    },
    //跳转类型
    navType: {
      type: String,
      value: "navigateTo",
      validator(val) {
        return ["switchTab", "navigateTo"].includes(val);
      },
    },
  },
  data: {
    style: "",
    isTranslateY: false,
    info: {
      iconWidth: "",
      icon: "",
      notice: "",
      tips: "",
      btnText: "",
      btnUrl: "",
    },
  },
  lifetimes: {
    attached() {
      const {
        top,
        iconWidth,
        type,
        icon,
        notice,
        tips,
        btnText,
        btnUrl,
      } = this.data;
      this.setData({
        style: `padding-top: ${topMap[top] || toCssVal(top)};`,
        isTranslateY: top === "pageCenter",
        info: {
          iconWidth: toCssVal(iconWidth),
          icon: icon || map[type]?.icon,
          notice: notice || map[type]?.notice,
          tips: tips || map[type]?.tips,
          btnText: btnText || map[type]?.btnText,
          btnUrl: btnUrl || map[type]?.btnUrl,
        },
      });
    },
  },
  methods: {
    onBtn(e) {
      const { to } = e.currentTarget.dataset;
      if (to) {
        if (to === "back") {
          const pages = getCurrentPages();
          if (pages.length > 1) {
            wx.navigateBack();
          } else {
            wx.switchTab({
              url: "/pages/home/home",
            });
          }
        } else if (to === "reload") {
          const page = getCurrentPages().pop();
          if (page) {
            page.onLoad();
            showToast("加载中……");
          }
        } else {
          const { navType } = this.data;
          wx[navType]({
            url: to,
          });
        }
      } else {
        console.error("跳转路径不能为空");
      }
    },
  },
});
